﻿using System;
using System.Drawing;
using System.Windows;
using System.Windows.Input;
using DPoint = System.Drawing.Point; 
using MBrush = System.Windows.Media.Brush;
using FCursor = System.Windows.Forms.Cursor;
using DSize = System.Drawing.Size;
using DPen = System.Drawing.Pen;
using DColor = System.Drawing.Color;
using RPAVIEW.util;
using System.Windows.Media;
using SUNRPA.CONTROLS.IMPL.util;
using RPAVIEW.views.EditPage.flows;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using System.IO;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using System.Collections.Generic;

namespace RPAVIEW.views.OtherPages {
    /// <summary>
    /// CutterWindow.xaml 的交互逻辑
    /// </summary>
    public partial class CutterWindow:Window {
        private Bitmap cutImg;
        private DPoint startPoint;
        private Boolean isStart = false;
        private Boolean isOver = false;
        private Rectangle catchRec;
        public CutterWindow(Bitmap cutImg) {
            InitializeComponent();
            this.cutImg = cutImg;
        }

        private void ExitCut(object sender, KeyEventArgs e) {
            if(e.Key == Key.Escape) { 
                this.Close();
            }
            if(e.Key == Key.Enter && isOver) { 
                //创建用户截取的范围大小的空图
                Bitmap cutArea = new Bitmap(catchRec.Width,catchRec.Height);
                Graphics g = Graphics.FromImage(cutArea);
                //在原始的屏幕截图ScreenBitmap上 截取 用户选择范围大小的区域   绘制到上面的空图
                //绘制完后，这个空图就是我们想要的截取的图片
                //参数1  原图
                //参数2  在空图上绘制的范围区域
                //参数3  原图的截取范围
                //参数4  度量单位
                g.DrawImage(cutImg,new Rectangle(0,0,cutArea.Width,cutArea.Height),catchRec,GraphicsUnit.Pixel);
                g.Dispose();
                string timestemp = DateUtil.GetTimeStemp();
                string savePath = ParamUtil.GetAppGlobleVar<Dictionary<string, string>>(DefaultContents.USER_DIRS)["picLibrary"] + Path.DirectorySeparatorChar + timestemp + ".jpg";
                cutArea.Save(savePath);
                cutArea.Dispose();
                ClickImgNParam param = WindowUtil.GetWindow<FlowEditWindow>().DataContext as ClickImgNParam;
                param.ImagePath = savePath;
                this.Close();
            }
            cutImg.Dispose();
            MainWindow mainWindow = WindowUtil.GetWindow<MainWindow>();
            WindowUtil.RecoverWindow(mainWindow);
            mainWindow.Activate();
            User32Utils.SetForegroundWindow(ElementUtil.getWindowPtr(mainWindow));
        }

        private void CutStart(object sender, MouseButtonEventArgs e) {
            if(isOver) {
                return;
            }
            startPoint = FCursor.Position;
            isStart = true;
        }

        private void CutOver(object sender, MouseButtonEventArgs e) {
            isStart = false;
            isOver = true;
        }

        private void ReCut(object sender, MouseButtonEventArgs e) {
            if(isOver) { 
                isStart = false;
                isOver=false;
                this.Background = new ImageBrush(ConverterUtil.Bitmap2ImageSource(cutImg));
            }
        }
         
        private void DrawCutRange(object sender, MouseEventArgs e) {
            if(isStart) {
                //计算路径范围
                int width = Math.Abs(FCursor.Position.X  - startPoint.X);
                int height = Math.Abs(FCursor.Position.Y - startPoint.Y);
                //
                //二次缓冲
                //不是直接在控件的背景画板上进行绘制鼠标移动路径，那样会造成绘制很多路径，因为前面绘制的路径还在
                //而是在内存中每移动一次鼠标就创建一张和屏幕截图一样的新BImtap,在这个Bitmap中绘制鼠标移动路径
                //然后在窗体背景画板上，绘制这个新的Bitmap,这样就不会造成绘制很多路径，因为每次都绘制了全新的Bitmao
                //但是这样做的话，因为鼠标移动的次数是大量的，所以在内存中会创建大量的Bitmap会造成内存消耗严重，所以每次移动绘制完后，
                //需要释放Dispose() 画板，画笔，Bitmap资源。
                //
                Bitmap cutBitMap = (Bitmap)cutImg.Clone(); //创建新的,在其上绘制路径
                //左上角
                DPoint firstP = new DPoint(startPoint.X,startPoint.Y);
                //新建画板，画笔
                Graphics g = Graphics.FromImage(cutBitMap);
                DPen p = new DPen(DColor.Red,1);
                if (FCursor.Position.X < startPoint.X) {
                    firstP.X = FCursor.Position.X;
                }
                if (FCursor.Position.Y < startPoint.Y) {
                    firstP.Y = FCursor.Position.Y;
                }
                //绘制路径
                catchRec = new Rectangle(firstP,new DSize(width,height));
                //将路径绘制在新的BItmap上，之后要释放
                g.DrawRectangle(p, catchRec);
                g.Dispose();
                p.Dispose();
                this.Background = new ImageBrush(ConverterUtil.Bitmap2ImageSource(cutBitMap));         
            }
        }
    }
}
